package com.company.oa.workflow.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.oa.common.api.Result;
import com.company.oa.workflow.dto.leave.LeaveDTO;
import com.company.oa.workflow.entity.leave.Leave;
import com.company.oa.workflow.service.LeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/workflow/leave")
public class LeaveController {
    
    @Autowired
    private LeaveService leaveService;
    
    /**
     * 分页查询请假列表
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('workflow:leave:list')")
    public Result<IPage<Leave>> getPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            Leave leave) {
        IPage<Leave> pageResult = leaveService.getPage(page, size, leave);
        return Result.success(pageResult);
    }
    
    /**
     * 获取请假详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('workflow:leave:info')")
    public Result<Map<String, Object>> getLeaveDetail(@PathVariable Long id) {
        Map<String, Object> detail = leaveService.getLeaveDetail(id);
        return Result.success(detail);
    }
    
    /**
     * 创建请假申请（草稿）
     */
    @PostMapping
    @PreAuthorize("hasAuthority('workflow:leave:create')")
    public Result<Boolean> createLeave(@RequestBody @Valid LeaveDTO leaveDTO) {
        // 从当前登录用户中获取用户ID
        Long userId = 1L; // 实际项目中应从SecurityContext中获取
        boolean success = leaveService.createLeave(leaveDTO, userId);
        return Result.success(success);
    }
    
    /**
     * 更新请假申请
     */
    @PutMapping
    @PreAuthorize("hasAuthority('workflow:leave:update')")
    public Result<Boolean> updateLeave(@RequestBody @Valid LeaveDTO leaveDTO) {
        boolean success = leaveService.updateLeave(leaveDTO);
        return Result.success(success);
    }
    
    /**
     * 提交请假申请
     */
    @PostMapping("/submit/{id}")
    @PreAuthorize("hasAuthority('workflow:leave:submit')")
    public Result<Boolean> submitLeave(@PathVariable Long id) {
        boolean success = leaveService.submitLeave(id);
        return Result.success(success);
    }
    
    /**
     * 取消请假申请
     */
    @PostMapping("/cancel/{id}")
    @PreAuthorize("hasAuthority('workflow:leave:cancel')")
    public Result<Boolean> cancelLeave(@PathVariable Long id) {
        boolean success = leaveService.cancelLeave(id);
        return Result.success(success);
    }
    
    /**
     * 上传附件
     */
    @PostMapping("/upload")
    @PreAuthorize("hasAuthority('workflow:leave:upload')")
    public Result<String> uploadAttachment(@RequestParam("file") MultipartFile file) {
        String filePath = leaveService.uploadAttachment(file);
        return Result.success(filePath);
    }
    
    /**
     * 获取我的请假列表
     */
    @GetMapping("/my")
    public Result<IPage<Leave>> getMyLeaves(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Integer status) {
        // 从当前登录用户中获取用户ID
        Long userId = 1L; // 实际项目中应从SecurityContext中获取
        IPage<Leave> pageResult = leaveService.getMyLeaves(page, size, userId, status);
        return Result.success(pageResult);
    }
}